package com.epam.Parsers;

import java.io.IOException;
import java.io.InputStream;
import java.util.LinkedList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import com.epam.Xml.Hotel;
import com.epam.Xml.TouristVoucher;

public class DOMParser {

	private static List<TouristVoucher> touristVouchers = new LinkedList<TouristVoucher>();
	private static InputStream is = DOMParser.class.getClassLoader().getResourceAsStream("NewFile.xml");
	//new File("src/main/resources/");
	public static List<TouristVoucher> parseXML() {
		TouristVoucher touristVoucher;
		Hotel hotel;

		try {
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			DocumentBuilder db = dbf.newDocumentBuilder();
			Document doc = db.parse(is);
			doc.getDocumentElement().normalize();

			NodeList nList = doc.getElementsByTagName("voucher");
			for (int i = 0; i < nList.getLength(); i++) {
				touristVoucher = new TouristVoucher();
				hotel = new Hotel();
				Node node = nList.item(i);
				if (node.getNodeType() == Node.ELEMENT_NODE) {
					Element element = (Element) node;
					touristVoucher.setId(Integer.parseInt(element
							.getAttribute("id")));
					touristVoucher.setCost(Double.parseDouble(element
							.getElementsByTagName("cost").item(0)
							.getTextContent()));
					touristVoucher.setCountry(Enum.valueOf(
							TouristVoucher.Country.class, element
									.getElementsByTagName("country").item(0)
									.getTextContent()));
					touristVoucher.setDuration(Integer.parseInt(element
							.getElementsByTagName("duration").item(0)
							.getTextContent()));
					touristVoucher.setTransport(Enum.valueOf(
							TouristVoucher.Transport.class, element
									.getElementsByTagName("transport").item(0)
									.getTextContent()));
					touristVoucher.setType(Enum.valueOf(
							TouristVoucher.Type.class, element
									.getElementsByTagName("type").item(0)
									.getTextContent()));
					hotel.setName(element.getElementsByTagName("name").item(0)
							.getTextContent());
					hotel.setCapacity(Integer.parseInt(element
							.getElementsByTagName("capacity").item(0)
							.getTextContent()));
					hotel.setConditioning(Boolean.parseBoolean(element
							.getElementsByTagName("conditioning").item(0)
							.getTextContent()));
					hotel.setFeeding(Enum.valueOf(Hotel.Feeding.class, element
							.getElementsByTagName("feeding").item(0)
							.getTextContent()));
					hotel.setStars(Integer.parseInt(element
							.getElementsByTagName("stars").item(0)
							.getTextContent()));
					hotel.setTv(Boolean.parseBoolean(element
							.getElementsByTagName("tv").item(0)
							.getTextContent()));
					touristVoucher.setHotel(hotel);
					touristVouchers.add(touristVoucher);
				}

			}

		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return touristVouchers;

	}

}
